package com.project.server.service.operate.impl;

import com.project.common.config.ProjectConfig;
import com.project.common.core.domain.BaseServer;
import com.project.common.core.domain.model.LoginUser;
import com.project.common.utils.SecurityUtils;
import com.project.common.utils.file.FileUploadUtils;
import com.project.framework.config.ServerConfig;
import com.project.server.domain.operate.TOperateContract;
import com.project.server.mapper.operate.TOperateContractMapper;
import com.project.server.service.operate.ITOperateContractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 运营管理合同Service业务层处理
 *
 * @author project
 * @date 2021-06-29
 */
@Service
public class TOperateContractServiceImpl extends BaseServer implements ITOperateContractService {

    private static final Logger log = LoggerFactory.getLogger(TOperateContractServiceImpl.class);

    @Resource
    private TOperateContractMapper tOperateContractMapper;

    @Resource
    private ServerConfig serverConfig;

    /**
     * 查询运营管理合同
     *
     * @param contractId 运营管理合同ID
     * @return 运营管理合同
     */
    @Override
    public TOperateContract selectTOperateContractById(Long contractId) {
        return tOperateContractMapper.selectTOperateContractById(contractId);
    }

    /**
     * 查询运营管理合同列表
     *
     * @param tOperateContract 运营管理合同
     * @return 运营管理合同
     */
    @Override
    public List<TOperateContract> selectTOperateContractList(TOperateContract tOperateContract) {
        List<TOperateContract> tOperateContracts = tOperateContractMapper.selectTOperateContractList(tOperateContract);
        return tOperateContracts;
    }

    /**
     * 新增运营管理合同
     *
     * @param tOperateContract 运营管理合同
     * @return 结果
     */
    @Override
    public int insertTOperateContract(MultipartFile[] file, TOperateContract tOperateContract) {
        super.insert(tOperateContract);
        // 上传文件路径
        String filePath = ProjectConfig.getUploadPath();
        List<String> urlList = new ArrayList<>(10);
        try {
            for (MultipartFile multipartFile : Arrays.asList(file)) {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, multipartFile);
                String url = serverConfig.getUrl() + fileName;
                urlList.add(url);
            }
            String collect = urlList.stream().collect(Collectors.joining(","));
            //储存文件地址
            tOperateContract.setContractEnclosure(collect);
            return tOperateContractMapper.insertTOperateContract(tOperateContract);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改运营管理合同
     *
     * @param tOperateContract 运营管理合同
     * @return 结果
     */
    @Override
    public int updateTOperateContract(MultipartFile[] file, TOperateContract tOperateContract) {
        super.update(tOperateContract);
        // 上传文件路径
        String filePath = ProjectConfig.getUploadPath();
        //数据库存储文件路劲
        List<String> urlList = new ArrayList<>(10);
        //总共上传的文件列表
        String fileList = tOperateContract.getFileList();
        //对比以前的和现在的文件是否发生变化，删除之前的文件列表中现在不存在的，保留现在文件列表中之前存在的
        String[] split1 = tOperateContract.getContractEnclosure().split(",");
        String[] split = fileList.split(",");
        for (String s : split) {
            for (String f : split1) {
                //保留这个文件地址
                if (f.contains(s)) {
                    urlList.add(f);
                }
            }
        }
        //新增的文件列表
        try {
            for (MultipartFile multipartFile : Arrays.asList(file)) {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, multipartFile);
                String url = serverConfig.getUrl() + fileName;
                urlList.add(url);
            }
            String collect = urlList.stream().collect(Collectors.joining(","));
            //更新地址
            tOperateContract.setContractEnclosure(collect);
            LoginUser loginUser = SecurityUtils.getLoginUser();
            //最后更新人
            tOperateContract.setUpdateBy(loginUser.getUsername());
            //最后更新时间
            tOperateContract.setUpdateTime(new Date());
            return tOperateContractMapper.updateTOperateContract(tOperateContract);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 批量删除运营管理合同
     *
     * @param contractIds 需要删除的运营管理合同ID
     * @return 结果
     */
    @Override
    public int deleteTOperateContractByIds(Long[] contractIds) {
        return tOperateContractMapper.deleteTOperateContractByIds(contractIds);
    }

    /**
     * 删除运营管理合同信息
     *
     * @param contractId 运营管理合同ID
     * @return 结果
     */
    @Override
    public int deleteTOperateContractById(Long contractId) {
        return tOperateContractMapper.deleteTOperateContractById(contractId);
    }
}
